package com.boruan.qq.zxgylibrary.ui.adapters;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.boruan.qq.zxgylibrary.R;
import com.boruan.qq.zxgylibrary.R2;
import com.boruan.qq.zxgylibrary.service.model.AddressListBean;
import com.boruan.qq.zxgylibrary.service.presenter.AddressPresenter;
import com.boruan.qq.zxgylibrary.ui.activities.AddAddressActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.boruan.qq.zxgylibrary.ui.activities.AddressListActivity.ADD_NEW_ADDRESS_CODE;

/**
 * Created by 崔琦 on 2019/8/12.
 * des: 地址适配器
 */

public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.AddressViewHolder> {

    private Activity mContext;
    private List<AddressListBean.ResultBean> mData;
    private AddressPresenter addressPresenter;
    private String shopId;

    public AddressAdapter(Activity mContext, String shopId, AddressPresenter addressPresenter) {
        this.mContext = mContext;
        this.addressPresenter = addressPresenter;
        this.shopId = shopId;
    }

    public void setData(List<AddressListBean.ResultBean> mData) {
        this.mData = mData;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public AddressViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.drug_item_address, parent, false);
        return new AddressViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AddressViewHolder holder, final int position) {
        holder.ivModifyAddress.setVisibility(View.VISIBLE);
        holder.tvUserAddressName.setText(mData.get(position).getFulladdress());
        String mSex = "";
        if (mData.get(position).getSex() == 1){
            mSex = "先生";
        }else if (mData.get(position).getSex() == 2){
            mSex = "女士";
        }
        if (mData.get(position).getIsdefault() == 1){
            holder.tvDefaultFlag.setVisibility(View.VISIBLE);
        }else {
            holder.tvDefaultFlag.setVisibility(View.GONE);
        }
        holder.tvUserNamePhone.setText(mData.get(position).getContactperson() + "(" + mSex + ")  " + mData.get(position).getContactphone());
        holder.ivModifyAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext,AddAddressActivity.class);
                intent.putExtra("type","modify");
                intent.putExtra("addressId",mData.get(position).getId());
                intent.putExtra("userName",mData.get(position).getContactperson());
                intent.putExtra("userPhone",mData.get(position).getContactphone());
                intent.putExtra("address",mData.get(position).getAddress());
                intent.putExtra("detailAddress",mData.get(position).getAttachaddress());
                intent.putExtra("fullAddress",mData.get(position).getFulladdress());
                intent.putExtra("isDefault",mData.get(position).getIsdefault());
                intent.putExtra("sex",mData.get(position).getSex());
                intent.putExtra("tag",mData.get(position).getTag());
                intent.putExtra("cityName",mData.get(position).getCityName());
                intent.putExtra("lat",mData.get(position).getLatitude());
                intent.putExtra("lng",mData.get(position).getLongitude());
                intent.putExtra("adCode",mData.get(position).getCitycode());
                if (mData.get(position).getFulladdress().contains(mData.get(position).getAddress())){
                    //老地址
                    intent.putExtra("isOldAddress",true);
                }else {
                    //新地址
                    intent.putExtra("isOldAddress",false);
                }
                mContext.startActivityForResult(intent,ADD_NEW_ADDRESS_CODE);
            }
        });

        holder.rlClickBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mData.get(position).getFulladdress().contains(mData.get(position).getAddress())){
                    //老地址,提示用户去修改
                    Toast.makeText(mContext, "由于版本更新，之前的老地址需要您修改一下，对您造成的不便深表歉意！", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(mContext,AddAddressActivity.class);
                    intent.putExtra("type","modify");
                    intent.putExtra("addressId",mData.get(position).getId());
                    intent.putExtra("userName",mData.get(position).getContactperson());
                    intent.putExtra("userPhone",mData.get(position).getContactphone());
                    intent.putExtra("address",mData.get(position).getAddress());
                    intent.putExtra("detailAddress",mData.get(position).getAttachaddress());
                    intent.putExtra("fullAddress",mData.get(position).getFulladdress());
                    intent.putExtra("isDefault",mData.get(position).getIsdefault());
                    intent.putExtra("sex",mData.get(position).getSex());
                    intent.putExtra("tag",mData.get(position).getTag());
                    intent.putExtra("cityName",mData.get(position).getCityName());
                    intent.putExtra("lat",mData.get(position).getLatitude());
                    intent.putExtra("lng",mData.get(position).getLongitude());
                    intent.putExtra("isOldAddress",true);
                    mContext.startActivityForResult(intent,ADD_NEW_ADDRESS_CODE);
                }else {
                    //新地址，直接判断
                    addressPresenter.judgeDelivery(mData.get(position).getId(),shopId,mData.get(position).getFulladdress(),mData.get(position).getContactperson() + " " + mData.get(position).getContactphone());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public class AddressViewHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.rl_click_back)
        RelativeLayout rlClickBack;
        @BindView(R2.id.tv_user_address_name)
        TextView tvUserAddressName;
        @BindView(R2.id.tv_user_name_phone)
        TextView tvUserNamePhone;
        @BindView(R2.id.iv_modify_address)
        ImageView ivModifyAddress;
        @BindView(R2.id.tv_default_flag)
        TextView tvDefaultFlag;
        public AddressViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
